package com.wny.activities;

import java.io.IOException;

import com.wny.R;
//import com.wny.String;
import com.wny.backend.SocketsClient;
import com.wny.backend.User;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class UserActivity extends Activity implements LocationListener{
	int uid;
	int otherUid;
	boolean friends;
	Location myLocation;
	LocationManager myProvider;
	public void onResume()
	{
		super.onResume();
		myProvider.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10f, this);
	}
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		myProvider = (LocationManager) getSystemService(LOCATION_SERVICE);
		Bundle extras = getIntent().getExtras();
		otherUid = extras.getInt("otherUid");
		friends = extras.getBoolean("friends");
		uid = extras.getInt("uid");
		
		if(friends==false){
			setContentView(com.wny.R.layout.userprofile);
		try {
			displayUserProfile(otherUid);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		}
		else{
			setContentView(com.wny.R.layout.friend_view);
			try {
				displayFriendView(otherUid);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		

	}
	public void displayFriendView(int user_uid) throws IOException{
		char [] user_info;
		String user_info_string;
		String the_user_name;
		String the_user_sex;
		String the_user_email;
		String the_user_status;
		String the_user_location;
		String the_user_latitude;
		String the_user_longitude;
		double latitude;
		double longitude;
		double otherLatitude = 0;
		double otherLongitude = 0;
		double distance;

		SocketsClient myComm = SocketsClient.getSocket();
		user_info=myComm.sendData(("get=user&uid="+user_uid+"\n").getBytes());
		user_info_string=String.valueOf(user_info);

		//the User class parses the necessary information
		User this_user = new User(user_info_string);


		the_user_name=this_user.getTheUserName();
		the_user_sex=this_user.getSex();
		the_user_email=this_user.getEmail();
		the_user_status=this_user.getStatus();
		the_user_latitude=this_user.getLatitude();
		the_user_longitude=this_user.getLongitude();
		
		if(myLocation==null){
			latitude=0;
			longitude=0;
		}
		else{
			latitude=myLocation.getLatitude();
			longitude=myLocation.getLongitude();
		}
		otherLatitude = Double.valueOf(the_user_latitude);
		otherLongitude = Double.valueOf(the_user_longitude);
		distance = Math.sqrt(Math.pow(otherLatitude - latitude, 2) + Math.pow(otherLongitude-longitude, 2));
		//distance= Math.sqrt( (Math.pow(Double.valueOf(the_user_latitude) - latitude, 2) + Math.pow(Double.valueOf(the_user_longitude)-longitude, 2)) );
		
		
		String the_distance=( String.valueOf(distance));
		TextView textdistance=((TextView)findViewById(R.id.frienddistance));
		TextView username=((TextView)findViewById(R.id.username));
		TextView usersex=((TextView)findViewById(R.id.usersex));
		TextView useremail=((TextView)findViewById(R.id.useremail));
		TextView userstatus=((TextView)findViewById(R.id.userstatus));
		

		//set the values for the appropriate fields
		textdistance.setText(the_distance);
		username.setText(the_user_name);
		usersex.setText(the_user_sex);
		useremail.setText(the_user_email);
		userstatus.setText(the_user_status);
		
	}
	private void addFriend(int uid)
	{
		char[] result=null;
		SocketsClient myComm = SocketsClient.getSocket();
		try {
			result = myComm.sendData(("add=friend&uid1="+this.uid+"&uid2="+uid+"\n").getBytes());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (result[0]=='0')
			displayWithToast("User already added.", 4000);
		else
			displayWithToast("User event to friends list", 4000);
	}
	private void removeFriend(int friendUid)
	{
		char[] result=null;
		SocketsClient myComm = SocketsClient.getSocket();
		try {
			result = myComm.sendData(("del=friend&uid1="+this.uid+"&uid2=" + friendUid +"\n").getBytes());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (result[0]=='0')
			displayWithToast("Friend already deleted", 4000);
		else
			displayWithToast("Friend deleted", 4000);
	}
	public boolean onCreateOptionsMenu(Menu menu) {
		// super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		//if (requestType == "myEvents")
		if (friends)
			inflater.inflate(R.menu.friend_menu,menu);
		else
			inflater.inflate(R.menu.user_menu, menu);
		return true;
	}
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.addFriend:
			addFriend(otherUid);
			return true;
		case R.id.remove_friend:
			removeFriend(otherUid);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	/**
	 * Display user profile.
	 *
	 * @param View v
	 * @param user_uid the user_uid
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public void displayUserProfile(int user_uid) throws IOException{

		char [] user_info;
		String user_info_string;
		String the_user_name;
		String the_user_sex;
		SocketsClient myComm = SocketsClient.getSocket();
		user_info=myComm.sendData(("get=user&uid="+user_uid+"\n").getBytes());
		user_info_string=String.valueOf(user_info);

		User this_user = new User(user_info_string);


		the_user_name=this_user.getTheUserName();
		the_user_sex=this_user.getSex();
		
		//get the username and sex
		TextView username=((TextView)findViewById(com.wny.R.id.username));
		TextView usersex=((TextView)findViewById(com.wny.R.id.usersex));

		username.setText(the_user_name);
		usersex.setText(the_user_sex);
	}
	// a wrapper function for the Toast class. Used to show messages onscreen
	private void displayWithToast(String toShow, int duration) {
		Toast temp = Toast.makeText(this, toShow, duration);
		temp.setGravity(Gravity.CENTER, 0, 0);
		temp.show();
	}
	public void onLocationChanged(Location location)
	{
		// TODO Auto-generated method stub
		myLocation = location;
		SocketsClient myComm = SocketsClient.getSocket();
		try {
			myComm.sendNonblocking(("loc&"+location.getLatitude()+"&"+location.getLongitude()+"&"+this.uid+"\n").getBytes());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	//notifies user GPS disables
	public void onProviderDisabled(String arg0) {
		// TODO Auto-generated method stub
		displayWithToast("The system GPS service has been disabled.", 5000);
	}

	//notifies user GPS enabled
	public void onProviderEnabled(String arg0) {
		// TODO Auto-generated method stub
		displayWithToast("GPS is now avaliable", 4000);

	}

	//notifies user of any change in the status of the GPS
	public void onStatusChanged(String location_provider, int status, Bundle otherVariables) {
		// TODO Auto-generated method stub
		switch (status)
		{
		case LocationProvider.OUT_OF_SERVICE:
			displayWithToast("The GPS is out of service.", 5000);
		case LocationProvider.TEMPORARILY_UNAVAILABLE:
			displayWithToast("The GPS is temporarially unavaliable", 5000);
		case LocationProvider.AVAILABLE:
			displayWithToast("GPS is now avaliable", 5000);
		}

	}
}
